package com.ssy.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssy.bus.mapper.DrugInfoMapper;
import com.ssy.bus.mapper.PrescriptionMapper;
import com.ssy.bus.pojo.DrugInfo;
import com.ssy.bus.pojo.Prescription;
import com.ssy.bus.service.IPrescriptionService;
import com.ssy.common.CodeMsg;
import com.ssy.common.utils.Result;
import com.ssy.system.mapper.InspectMapper;
import com.ssy.system.pojo.Inspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author ssy
 */
@Service
public class PrescriptionServiceImpl extends ServiceImpl<PrescriptionMapper, Prescription> implements IPrescriptionService {

    @Autowired
    private PrescriptionMapper prescriptionMapper;

    @Autowired
    private DrugInfoMapper drugInfoMapper;

    @Autowired
    private InspectMapper inspectMapper;


    @Override
    public Result addDrugPrescription(Prescription prescription) {
        Integer[] ids = prescription.getIds();
        if (ids == null || ids.length == 0) {
            return new Result(CodeMsg.ADD_ERROR);
        }
        String outpatientId = prescription.getOutpatientId();
        for (Integer id : ids) {
            this.prescriptionMapper.insert(new Prescription(outpatientId, id, 1, 1, 2, "照说明服用"));
        }
        return new Result(CodeMsg.ADD_SUCCESS);
    }

    @Override
    public Result addInspectPrescription(Prescription prescription) {
        Integer[] ids = prescription.getIds();
        if (ids == null || ids.length == 0) {
            return new Result(CodeMsg.ADD_ERROR);
        }
        String outpatientId = prescription.getOutpatientId();
        for (Integer id : ids) {
            this.prescriptionMapper.insert(new Prescription(outpatientId, id, 2, 1, 2, "无"));
        }
        return new Result(CodeMsg.ADD_SUCCESS);
    }

    @Override
    public Result loadDrugInfoTable(String outpatientId) {
        List<Prescription> prescriptions = this.prescriptionMapper.selectList(new LambdaQueryWrapper<Prescription>()
                .eq(Prescription::getType, 1)
                .eq(Prescription::getOutpatientId, outpatientId));

        for (Prescription prescription : prescriptions) {
            Integer preId = prescription.getPreId();
            DrugInfo drugInfo = drugInfoMapper.selectById(preId);
            prescription.setDrugId(drugInfo.getId());
            prescription.setDrugName(drugInfo.getName());
            prescription.setPrice(drugInfo.getPrice());
            BigDecimal number = new BigDecimal("" + prescription.getNumber());
            prescription.setMoney(number.multiply(drugInfo.getPrice()));
        }

        return new Result(prescriptions);
    }

    @Override
    public Result loadInspectTable(String outpatientId) {
        List<Prescription> prescriptions = this.prescriptionMapper.selectList(new LambdaQueryWrapper<Prescription>()
                .eq(Prescription::getType, 2)
                .eq(Prescription::getOutpatientId, outpatientId));
        for (Prescription prescription : prescriptions) {
            //获取检查项ID
            Integer preId = prescription.getPreId();
            Inspect inspect = inspectMapper.selectById(preId);
            prescription.setInspectId(inspect.getId());
            prescription.setInspectName(inspect.getName());
            prescription.setCompany(inspect.getCompany());
            prescription.setPrice(inspect.getPrice());
            prescription.setMoney(inspect.getPrice());
        }
        return new Result(prescriptions);
    }

    @Override
    public Result deletePrescription(String outpatientId, Integer id, Integer type) {
        int i = this.prescriptionMapper.delete(new LambdaQueryWrapper<Prescription>()
                .eq(Prescription::getOutpatientId, outpatientId)
                .eq(Prescription::getPreId, id)
                .eq(Prescription::getType, type));
        return i > 0 ? new Result(CodeMsg.DELETE_SUCCESS) : new Result(CodeMsg.DELETE_ERROR);
    }

    @Override
    public Result updatePrescriptionByDrug(String outpatientId, Integer drugId, Integer number) {
        Prescription prescription = new Prescription();
        prescription.setNumber(number);
        int i = this.prescriptionMapper.update(prescription, new LambdaQueryWrapper<Prescription>()
                .eq(Prescription::getOutpatientId, outpatientId)
                .eq(Prescription::getPreId, drugId));

        return i > 0 ? new Result(CodeMsg.UPDATE_SUCCESS) : new Result(CodeMsg.UPDATE_ERROR);
    }

}
